package com.fashion.web.actions.gallery;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.stereotype.Controller;

import com.fashion.model.gallery.Album;
import com.fashion.model.gallery.GalleryColumn;
import com.fashion.service.gallery.AlbumService;
import com.fashion.service.gallery.GalleryColumnService;
import com.opensymphony.xwork2.ActionSupport;

@Controller
@Namespace("/gallery")
public class ColumnAction extends ActionSupport {
	private Logger logger = Logger.getLogger(ColumnAction.class);
	@Resource(name = "galleryColumnService")
    private GalleryColumnService galleryColumnService;
	@Resource(name = "albumService")
	private AlbumService albumService;
	private List<GalleryColumn> columns = new ArrayList<GalleryColumn>();
	private GalleryColumn column;
	private List<Album> albums;
	private List<Album> recommendAlbums;
	private String coverType;
	private int pageNum = 1;
	private int pageCount;
	private int albumCountInPage = 9;

	public String getCoverType() {
		return coverType;
	}

	public void setCoverType(String coverType) {
		this.coverType = coverType;
	}


	public void setColumn(GalleryColumn column) {
		this.column = column;
	}

	public GalleryColumn getColumn() {
		return column;
	}

	@Action(value = "column", results = {@Result(name = SUCCESS, location = "column.jsp")})
    public String index() throws Exception {
		recommendAlbums = albumService.find(null, null, "priority", 0, 8);
		columns = galleryColumnService.format(galleryColumnService.findAll());
    	column =  galleryColumnService.findColumnFromTree(columns, this.column);
    	List<GalleryColumn> children = new ArrayList<GalleryColumn>();
		children.addAll(galleryColumnService.getAllChildren(column));
		List<String> allColumns = new ArrayList<String>();
		allColumns.add(column.getId());
		for(int j = 0; j < children.size(); j++){
            allColumns.add(children.get(j).getId());
        }
		logger.info("pageNum: " + pageNum);
        albums = albumService.findByColumn(allColumns, (pageNum - 1)*albumCountInPage, albumCountInPage);
        pageCount = albumService.countByColumn(allColumns)/albumCountInPage + 1;
        return SUCCESS;
    }

	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	public int getPageCount() {
		return pageCount;
	}

	public List<GalleryColumn> getColumns() {
		return columns;
	}

	public List<Album> getAlbums() {
		return albums;
	}

	public List<Album> getRecommendAlbums() {
		return recommendAlbums;
	}
	
	
	
	
}
